﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using Fluid.Controls;
using TouchRC.Classes;
using System.Xml.Linq;

namespace TouchRC.Logic
{
    public static class AppSettings
    {
        private static Rectangle m_TempHack = new Rectangle(0, 0, 240, 400);
        public static Rectangle ScreenResolution { get { return m_TempHack; } set { m_TempHack = value; } }

        public static bool IsWidescreen { get; set; }

        public static int BaseControlHeight = 35;// (int)(35 * FluidHost.Instance.ScaleFactor.Width);
        public static int ButtonDimensions = 35;//(int)(35 * FluidHost.Instance.ScaleFactor.Width);

        public static ScreenOrientation CurrentOrientation
        {
            get
            {
                return ScreenResolution.Width < ScreenResolution.Height ? ScreenOrientation.Portrait : ScreenOrientation.Landscape;
            }
        }

        public static bool BacklightAlwaysOn { get; private set; }
        public static bool Log { get; private set; }

        public static DateTime ApplicationStartTime { get; private set; }

        public static void Aquire()
        {
            ApplicationStartTime = DateTime.Now;

            XDocument settingsDoc = XDocument.Load(ConfigSettings.SettingsFile.FullName);

            bool defaultBacklight = true;
            bool defaultLog = false;

            var settings = from bob in settingsDoc.Descendants("UserSettings")
                           select new XmlSettings
                                {
                                    BacklightAlwaysOn = GetBoolFromXElement(bob.Element("BacklightAlwaysOn"), defaultBacklight),
                                    Logger = GetBoolFromXElement(bob.Element("Log"), defaultLog)
                                };

            XmlSettings xmlSettings = settings.FirstOrDefault();
            BacklightAlwaysOn = xmlSettings.BacklightAlwaysOn;
            Log = xmlSettings.Logger;
        }

        private static bool GetBoolFromXElement(XElement element, bool defaultValue)
        {
            bool retVal = defaultValue;
            try
            {
                retVal = bool.Parse(element.Value);
            }
            catch
            {
            }
            return retVal;
        }

        private class XmlSettings
        {
            public bool BacklightAlwaysOn = true;
            public bool Logger = false;
        }
    }
}
